package org.loofer.ext.component.drawable;

import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;

public class FrameAnimationDrawable extends AnimationDrawable {
    private int mHeight;
    private int mTotalDuration;
    private int mWidth;

    public FrameAnimationDrawable() {
        this.mWidth = -1;
        this.mHeight = -1;
        this.mTotalDuration = 0;
        init();
    }

    public FrameAnimationDrawable(Drawable first, int duration) {
        this();
        addFrame(first, duration);
    }

    private void init() {
        setOneShot(false);
        setVisible(true, false);
    }

    private void computeSizeIfNeeded() {
        if (this.mWidth <= 0 || this.mHeight <= 0) {
            Drawable first = getFrame(0);
            if (first != null) {
                this.mWidth = first.getIntrinsicWidth();
                this.mHeight = first.getIntrinsicHeight();
            }
        }
    }

    public void addFrame(Drawable frame, int duration) {
        super.addFrame(frame, duration);
        computeSizeIfNeeded();
        this.mTotalDuration += duration;
    }

    public int getTotalDuration() {
        return this.mTotalDuration;
    }

    public int getIntrinsicWidth() {
        return this.mWidth;
    }

    public int getIntrinsicHeight() {
        return this.mHeight;
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
        start();
    }
}
